package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.AppealDTO;
import com.sky.dto.AppealPageQueryDTO;
import com.sky.entity.Appeal;
import com.sky.entity.ViolationRecord;
import com.sky.mapper.AppealMapper;
import com.sky.mapper.ViolationRecordMapper;
import com.sky.result.PageResult;
import com.sky.service.AppealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AppealServiceImpl implements AppealService {
    @Autowired
    private AppealMapper appealMapper;
    @Autowired
    private ViolationRecordMapper  violationRecordMapper;

    @Override
    public Appeal getById(Long id) {
        Appeal appeal= appealMapper.getById(id);
        return appeal;
    }

    @Override
    public PageResult pageQuery(AppealPageQueryDTO pageQuery) {
        //1、设置当前页和每页条数
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());
        Page<Appeal> page =appealMapper.pageQuery(pageQuery);
        long total = page.getTotal();
        List<Appeal> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public PageResult mypageQuery(AppealPageQueryDTO pageQuery) {
        //1、设置当前页和每页条数
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize());
        Page<Appeal> page =appealMapper.mypageQuery(pageQuery);
        long total = page.getTotal();
        List<Appeal> records = page.getResult();
        return new PageResult(total,records);
    }
     @Override
    public void appeal(AppealDTO appealDTO) {
        //1、构建appeal
        Appeal appeal = new Appeal();
        //通过查询违法记录来为申诉记录赋值
        ViolationRecord violationRecord=violationRecordMapper.getById(appealDTO.getViolationId());
        appeal.setId(appealDTO.getId());

        appeal.setViolationId(violationRecord.getId());
        appeal.setType(violationRecord.getType());
        appeal.setViolationType(violationRecord.getViolationType());
        appeal.setImagePath(violationRecord.getImagePath());
        appeal.setLocation(violationRecord.getLocation());
        appeal.setStatus(Appeal.TO_BE_CONFIRMED);
        appeal.setBeginTime(violationRecord.getCreateTime());
        appeal.setCreateTime(LocalDateTime.now());
        appeal.setCreateUser(BaseContext.getCurrentId());
        appeal.setFile(appealDTO.getFile());
        appeal.setReason(appealDTO.getReason());
        appeal.setCarId(appealDTO.getCarId());
        //2、调用mapper写入数据库
        appealMapper.insert(appeal);
    }
    
    @Override
    public void process(AppealDTO appealDTO) {
        //在弹窗中显示申诉证据与申诉原因
        Appeal appeal=appealMapper.getById(appealDTO.getId());
        //选择申诉处理状态
        appeal.setStatus(appealDTO.getStatus());
        //填写申诉处理理由
        appeal.setDetail(appealDTO.getDetail());
        //修改数据库中申诉记录
        appealMapper.update(appeal);



    }
}
